/**
 * 
 */
package com.cuc.platform.core.componet.security.service.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cuc.platform.core.componet.security.dao.SecurityMenuDao;
import com.cuc.platform.core.componet.security.domain.SecurityMenu;
import com.cuc.platform.core.componet.security.service.SecurityMenuService;
import com.cuc.platform.core.componet.security.utils.SecurityIdentityTools;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 *
 * 菜单业务实现类
 */
@Service("SecurityMenuService")
public class SecurityMenuServiceImpl implements SecurityMenuService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#getAllMenus()
	 */
	@Override
	public List<SecurityMenu> getAllMenus() {
		return securityMenuDao.getAllMenus();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#getMenuById(java.lang.Long)
	 */
	@Override
	public SecurityMenu getMenuById(Long id) {
		if (null == id) return null;
		
		return securityMenuDao.getMenuById(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#insertMenu(com.cuc.platform.core.componet.security.domain.SecurityMenu)
	 */
	@Override
	public void insertMenu(SecurityMenu menu) throws ServiceException {
		if (null == menu) return;
		
		/* 赋默认值 */
		//非系统管理级菜单
		menu.setIsAdmin(SecurityIdentityTools.NO_ADMIN);
		
		securityMenuDao.insertMenu(menu);	
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#updateMenu(com.cuc.platform.core.componet.security.domain.SecurityMenu)
	 */
	@Override
	public void updateMenu(SecurityMenu menu) throws ServiceException {
		if ((null == menu) || (null == menu.getId())) return;
		
		//非系统管理级菜单
		menu.setIsAdmin(SecurityIdentityTools.NO_ADMIN);
		
		securityMenuDao.updateMenu(menu);	
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#delMenu(java.lang.Long)
	 */
	@Override
	public void delMenu(Long id) throws ServiceException {
		if (null == id) return;
		
		int rv = securityMenuDao.delMenuById(id);
		
		//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
		if(rv == 0){
			logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
			throw new ServiceException("系统中不存在要删除的数据！");
		}
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#listMenuByLevel(java.lang.Integer)
	 */
	@Override
	public List<SecurityMenu> listMenuByLevel(Integer level) {
		if(null == level) return null;
		
		return securityMenuDao.listMenuByLevel(level);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#listSubMenu(java.lang.Long)
	 */
	@Override
	public List<SecurityMenu> listSubMenu(Long id) {
		if (null == id) return null;
		
		return securityMenuDao.listSubMenu(id);
	}	
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#viewRoleswithMenus(java.lang.String)
	 */
	@Override
	public List<SecurityMenu> viewRoleswithMenus(Long menu_id) {
		if (null == menu_id) return null;
		
		return securityMenuDao.viewRoleswithMenus(menu_id);
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#viewMenuswithRoles(java.lang.String)
	 */
	@Override
	public List<SecurityMenu> viewMenuswithRoles(Long role_id) {
		if (null == role_id) return null;
		
		return securityMenuDao.viewMenuswithRoles(role_id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#saveRoleMenuUnions(java.lang.String, java.lang.String[])
	 */
	@Override
	public void saveRoleMenuUnions(String role_id, String[] menu_ids) throws ServiceException {
		if ((null == role_id)||(role_id.equals(""))) return;
	
		//首先删除该role_id对应的关联关系，以避免插入重复数据，然后再循环插入。
		securityMenuDao.delRoleMenuUnions(Long.valueOf(role_id));
		
		if (null == menu_ids ) return;

		/*插入多条关联数据*/
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("role_id", Long.valueOf(role_id));
		for(String menu_id : menu_ids){
			map.put("menu_id", Long.valueOf(menu_id));
			securityMenuDao.saveRoleMenuUnions(map);
		}		
	}	
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityMenuService#saveMenuRoleUnions(java.lang.String, java.lang.String[])
	 */
	@Override
	public void saveMenuRoleUnions(String menu_id, String[] role_ids) throws ServiceException {
		if ((null == menu_id)||(menu_id.equals(""))) return;
		
		//删除该menu_id对应的关联关系
		securityMenuDao.delRoleMenuUnionsByMenu(Long.valueOf(menu_id));
		
		if (null == role_ids ) return;

		/*插入多条关联数据*/
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("menu_id", Long.valueOf(menu_id));
		for(String role_id : role_ids){
			map.put("role_id", Long.valueOf(role_id));
			securityMenuDao.saveRoleMenuUnions(map);
		}		
	}	
	
	@Override
	public List<SecurityMenu> listMenuByUser(String userName) {
		return securityMenuDao.listMenuByUser(userName);
	}
	
	
	/**
	 * 获取具有权限的某个级别菜单项列表
	 * 
	 * 注：不是某级别所有的菜单项，而是具有权限的列表
	 * 
	 *  @param userName 登录账号
	 *  @parem level 菜单级别
	 */
	public List<SecurityMenu> listLevelByUser(String userName, Integer level){
		if (null == userName || userName.equals("")) return null;
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userName", userName);
		paramMap.put("level", level);
		
		return securityMenuDao.listMenuByUserWithLevel(paramMap);
	}
	
	
	//日志
	private static Logger logger = Logger.getLogger(SecurityMenuServiceImpl.class);
	
	/**
	 * Ioc
	 */
	@Resource
	private SecurityMenuDao securityMenuDao;
}
